package com.xxy.book.entity;

/**
 * @Author Xu XianYun
 * @Date 2022-12-18
 * @Description
 */
public class Page {

    //当前页码
    private int current = 1;

    //单页显示的记录上限
    private int size = 10;

    //总行数(用于计算多少页)
    private int rows;

    //查询路径 (复用分页链接)
    private String path;

    /* 设置当前页码 */
    public void setCurrent(int current) {
        if (current >= 1) {
            this.current = current;
        }
    }

    /* 获取当前页码 */
    public int getCurrent() {
        return current;
    }

    public void setSize(int size) {
        if(current >= 1 && size <= 100) {
            this.size = size;
        }
    }

    public int getSize() {
        return size;
    }

    public void setRows(int rows) {
        if (rows > 0) {
            this.rows = rows;
        }
    }

    public int getRows() {
        return rows;
    }

    /**
     *  获取当前页的起始索引 offset
     *  (页数 - 1) * 每页显示记录上限
     */
    public int getOffset() {
        return (current - 1) * size;
    }

    /**
     * 获取总页数
     */
    public int getTotal() {
        if (rows % size == 0) {
            return rows / size;
        } else {
            return rows / size + 1;
        }
    }

    /**
     * 获取分页栏起始页码
     * 分页栏显示当前页码和前后两页页码
     */
    public int getFrom() {
        int from = current - 2;
        return from < 1 ? 1 : from;
    }

    /**
     * 获取分页栏尾页码
     *
     */
    public int getTo() {
        int to = current + 2;
        int total = getTotal();
        return to > total ? total : to;
    }
}
